// pages/phone/phone.js
const app = getApp();
const utils = require('../../utils/util.js');
const post = utils.post;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        phoneNumber: '',
        ifyzm: false,
        phoneCode: '',
        time: '获取验证码',
        currentTime: 60,
        ifChannel: false,
        is_ios_btn: false,
        ifStatus: false,
        ifad: 1,
        show_info_img: '',
        show_info_url: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        let p = {
            m: 'weapp_status',
            v: app.globalData.v
        };
        post(p).then(res => {
            if (1 == res.status) { // 审核通过了
                that.setData({
                    ifStatus: true
                });
            }
        })

        // 广告切换
        let pp = {
            m: 'show_info'
        };
        post(pp).then(res => {
            if (res.status == 2) {
                that.setData({
                    ifad: res.status,
                    show_info_img: res.show_info_img,
                    show_info_url: res.show_info_url
                })
            }
        })

        wx.setStorageSync('ifList', true);
        if (!!options.channel) {
            that.setData({
                channel: options.channel
            })
        }
        if (!!options.t_uid) {
            that.setData({
                t_uid: options.t_uid
            })
        }
    },
    phoneNumber(e) {
        let that = this;
        if (e.detail.value !== 0) {
            var reg = /^1(3|5|7|8|9)\d{9}$/;
            if (reg.test(e.detail.value)) {
                that.setData({
                    ifyzm: true,
                    phoneNumber: e.detail.value
                })
            } else {
                that.setData({
                    ifyzm: false
                })
            }
        }
    },
    phoneCode(e) {
        let that = this;
        that.setData({
            phoneCode: e.detail.value
        })
    },
    getCode() {
        let that = this;
        var interval = null
        if (that.data.phoneNumber.length == 0) {
            wx.showToast({
                title: '手机号为空！',
                duration: 2000,
                icon: 'none',
            })
        };
        var reg = /^1(3|5|7|8|9)\d{9}$/;
        if (reg.test(that.data.phoneNumber)) {
            var p = {
                m: 'send_code',
                phone: that.data.phoneNumber
            };
            post(p).then(res => {
                // console.log(res);
                if (res.r == 0) {
                    wx.showToast({
                        title: res.errmsg,
                        duration: 2000,
                        icon: 'success',
                    })
                }
            });
        } else {
            wx.showToast({
                title: '您输入的手机号有误',
                duration: 2000,
                icon: 'none',
            })
        }
        var currentTime = that.data.currentTime;
        that.setData({
            time: currentTime + '秒',
            ifyzm: false,
        })
        interval = setInterval(function() {
            that.setData({
                time: (currentTime - 1) + '秒',

            })
            currentTime--;
            if (currentTime <= 0) {
                clearInterval(interval)
                that.setData({
                    time: '重新获取',
                    currentTime: 60,
                    disabled: false,
                    ifyzm: true,
                })
            }
        }, 1000)
    },
    phone_login() {
        let that = this;
        if (that.data.phoneNumber.length !== 0) {
            if (that.data.phoneCode.length !== 0) {
                if (!!that.data.channel || !!that.data.t_uid) {
                    let p = {
                        m: 'check_code',
                        phone: that.data.phoneNumber,
                        code: that.data.phoneCode
                    };
                    if (!!that.data.t_uid) {
                        p.t_uid = that.data.t_uid
                    }
                    if (!!that.data.channel) {
                        p.channel = that.data.channel
                    }
                    post(p).then(res => {
                        // console.log(res);
                        if (res.r == 0) {
                            wx.showToast({
                                title: res.errmsg,
                                duration: 2000,
                                icon: 'none',
                            });
                            wx.setStorageSync("z_uid", res.z_uid);
                            wx.setStorageSync("m_uid", res.m_uid);
                        }
                        wx.getSystemInfo({
                            success: function(res1) {
                                let dataList = res1.system.toLowerCase(); //获取到的数据
                                if (/^ios/.test(dataList)) {
                                    that.setData({
                                        is_ios_btn: true
                                    });
                                } else if (/^android/.test(dataList)) {
                                    that.setData({
                                        is_ios_btn: false
                                    });
                                    console.log(res.d_path);
                                    if (!!res.d_path) {
                                        wx.navigateTo({
                                            url: '/pages/webview/webview?path=' + res.d_path
                                        });
                                    }
                                }
                            }
                        });
                    })
                } else {
                    wx.showModal({
                        content: '缺少信息，无法注册',
                        showCancel: false
                    });
                }
            } else {
                wx.showToast({
                    title: '验证码为空！',
                    duration: 2000,
                    icon: 'none',
                });
            }
        } else {
            wx.showToast({
                title: '手机号为空！',
                duration: 2000,
                icon: 'none',
            });
        }


    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        let that = this;
        that.setData({
            is_ios_btn: true
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    /*onShareAppMessage: function() {

    }*/
    submitFormId(e) {
        let formId = e.detail.formId;
        let button = e.detail.target.dataset.name;
        var m_uid = wx.getStorageSync("weapp_uid");
        let page = getCurrentPageUrlWithArgs();
        let p = {
            m: 'colform',
            uid: m_uid,
            openid: wx.getStorageSync('weapp_openid'),
            formId: formId,
        }
        if (page && page.length > 0) {
            p.page = page;
        }
        if (button && button.length > 0) {
            p.button = button;
        }
        post(p).then(res => {})
    }
})